// pages/form-subscribe/index.js
import { checkPhone, handlerGobackClick, hint, onlyChangeRouter } from "../../../utils/util";
import { Subscribe } from "../../../models/subscribe";

const subscribe = new Subscribe();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ownerName: "",
    ownerPhone: "",
    visitorName: "",
    visitorIdCard: "",
    visitorPhone: "",
    relation: "",
    time: "",
    remark: "",
    commid: "",
    communityName: "",
    isClick: true,
    isShow: false,
    communityList: [],
    searchValue: "",
    actions: [],
    isRelation: false,
    isTime: false,
    isRemark: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.message) {
      this.setData({
        remark: options.message,
      });
    }
    if (options.id) {
      console.log("加载数据");
      this.initPageData(options.id);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  bindInput(e) {
    let key = e.currentTarget.dataset.key;
    this.setData({
      [key]: e.detail,
    });
  },
  onlyChangeRouter,
  handlerGobackClick,
  selectCommunity() {
    let ownerPhone = checkPhone(this.data.ownerPhone);
    if (ownerPhone !== true) {
      hint(ownerPhone);
      return;
    }
    this.setData({
      isShow: true,
    });
    // wx.navigateTo({
    // 	url: `/owner-app/pages/select-community/index?phone=${this.data.ownerPhone}&commid=${this.data.commid}&type=selectCommunity`
    // })
  },
  searchChange(e) {
    subscribe
      .getCommunityList(this.data.ownerPhone, e.detail)
      .then((res) => {
        this.setData({
          communityList: res.result,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  itemClick(e) {
    this.setData({
      commid: e.target.dataset.commid,
      isShow: false,
      communityName: e.target.dataset.communityname,
      communityList: [],
      searchValue: "",
    });
  },
  onChange(e) {
    let key = e.currentTarget.dataset.key;
    let value;
    if (key === "ownerPhone" || key === "visitorPhone") {
      value = e.detail.replace(/\s*/g, "");
    } else {
      value = e.detail;
    }
    this.setData({
      [key]: value,
    });
  },
  goApply() {
    if (!this.data.ownerName) {
      hint("请输入业主姓名");
      return;
    }
    let checkOwnerPhone = checkPhone(this.data.ownerPhone);
    if (checkOwnerPhone !== true) {
      hint(checkOwnerPhone);
      return;
    }
    if (!this.data.commid) {
      hint("小区名称不能为空");
      return;
    }
    if (!this.data.visitorName) {
      hint("请输入姓名");
      return;
    }
    let regex = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    if (!regex.test(this.data.visitorIdCard)) {
      hint("身份证号错误，请重试");
      return;
    }
    let visitorPhone = checkPhone(this.data.visitorPhone);
    if (visitorPhone !== true) {
      hint(visitorPhone);
      return;
    }
    if (!this.data.relation) {
      hint("与业主关系不能为空");
      return;
    }
    if (!this.data.time) {
      hint("访问期限不能为空");
      return;
    }
    if (!this.data.remark) {
      hint("来访事由不能为空");
      return;
    }
    let {
      ownerName,
      ownerPhone,
      commid,
      visitorName,
      visitorIdCard,
      visitorPhone: editVisitorPhone,
      relation: visitorRelation,
      time: visitorOpenTime,
      remark,
    } = this.data;
    if (this.data.isClick) {
      this.setData({
        isClick: false,
      });
      // commid="610429001001001"  
      subscribe
        .putSubscribe(
          ownerName,
          ownerPhone,
          commid,
          visitorName,
          remark,
          visitorIdCard,
          editVisitorPhone,
          visitorRelation,
          visitorOpenTime
        )
        .then((res) => {
          this.setData({
            isClick: true,
          });
          hint("提交成功！");
          wx.redirectTo({
            url: "/owner-app/pages/subscribe/index",
          });
        })
        .catch((err) => {
          this.setData({
            isClick: true,
          });
          console.log(err);
        });
    }
  },
  // inputRemark() {
  // 	wx.navigateTo({
  // 		url: `/owner-app/pages/remark/index?remark=${this.data.remark}`
  // 	})
  // },
  //业主/期限/事由
  selectValue(e) {
    let key = e.target.dataset.key;
    let index = e.target.dataset.index;
    this.setData({
      [key]: true,
      actions: [],
    });
    subscribe
      .getDictItem(index)
      .then((res) => {
        let result = res.result;
        let arr = [];
        result.forEach((v, i) => {
          let value = { name: v.text };
          arr.push(value);
        });
        this.setData({
          actions: arr,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  initPageData(id) {
    subscribe
      .auditListById(id)
      .then((res) => {
        console.log(res);
        let { ownerName, ownerPhone, visitorName, commid, remark } = res.messageBody;
        let communityName = res.messageBody.commname;
        this.setData({
          ownerName,
          ownerPhone,
          visitorName,
          commid,
          remark,
          communityName,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  //关闭小区弹出框
  controllPopup() {
    this.setData({
      isShow: false,
      communityList: [],
      searchValue: "",
    });
  },
  //取消选择
  cancel(e) {
    let key = e.target.dataset.key;
    this.setData({
      [key]: false,
    });
  },
  selected(e) {
    let value;
    let selectValue = e.detail.name;
    let key = e.target.dataset.key;
    switch (key) {
      case "isRelation":
        value = "relation";
        break;
      case "isTime":
        value = "time";
        break;
      case "isRemark":
        value = "remark";
        break;
    }
    this.setData({
      [key]: false,
      [value]: selectValue,
    });
  },
});
